using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.ComponentModel;


namespace Arche.Web.UI.WebControls
{

	/// <summary>
	/// A clientscript-enabled radiobutton which toggles the visibility of another control on the page.
	/// </summary>
	/// <remarks>
	/// This radiobutton has no server-side events, as the action it performs is done on the clientside.
	/// </remarks>
	[
	DefaultProperty("Text"),
	]
	public class ExpandingRadioButton : System.Web.UI.WebControls.RadioButton, INamingContainer 
	{
		
		#region Properties
		/// <summary>
		/// Gets or sets the ID of the control which is the target of the visibility-toggling behavior.
		/// </summary>
		[
		Description("Gets or sets the ID of the control which is the target of the visibility-toggling behavior."),
		Category("Behavior"),
		DefaultValue(""),
		Bindable(true),
		]
		public virtual String ControlToToggle 
		{
			get 
			{
				object savedState = this.ViewState["ControlToToggle"];
				if ( savedState != null ) 
				{
					return (String)savedState; 
				}
				else 
				{
					return "";
				}
			}
			set 
			{
				this.ViewState["ControlToToggle"] = value;
				this.cachedTargetControl = null;
			}
		}

		/// <summary>
		/// Gets or sets the expanded state of the target control.
		/// </summary>
		[
		Description("Gets or sets the expanded state of the target control."),
		Category("Appearance"),
		DefaultValue(true),
		Bindable(true),
		]
		public virtual Boolean Expanded 
		{
			get 
			{
				return Checked;
			}
			set 
			{
				
				Checked = value;
			}
		}



		/// <summary>
		/// Gets or sets the textalign of the radiobutton.
		/// </summary>
		[
		Description("Gets or sets the textalign of the radiobutton."),
		Category("Appearance"),
		DefaultValue(""),
		Bindable(true),
		]

		
		#endregion

		#region Render Path
		protected override void OnPreRender(EventArgs e) 
		{
			base.OnPreRender (e);
			this.RegisterClientScript();
			this.ApplyExpansionState();
		}
		#endregion

		/// <summary>
		/// Overrides <see cref="Control.Controls"/>.
		/// </summary>
		public override ControlCollection Controls 
		{
			get 
			{				
				return base.Controls;
			}
		}


		/// <summary>
		/// Applies the <see cref="Expanded"/> value to the server control set by <see cref="ControlToToggle"/>.
		/// </summary>
		protected virtual void ApplyExpansionState() 
		{

			if ( this.Expanded ) 
			{
				if ( this.targetStyle["display"] != null ) 
				{
					this.targetStyle.Remove("display");
				}
			} 
			else 
			{
				this.targetStyle["display"] = "none";
			}
		}




		/// <summary>
		/// Registers the clientscript with the page.
		/// </summary>
		protected virtual void RegisterClientScript() 
		{
			ExpandingButtonScriptUtil.RegisterScriptForControl(this ,this.targetControl,null,"true","false");
		}


		private Control targetControl 
		{
			get 
			{
				if ( cachedTargetControl == null ) 
				{
					this.cachedTargetControl = this.NamingContainer.FindControl(this.ControlToToggle);
				}
				return this.cachedTargetControl;
			}
		}
		private Control cachedTargetControl = null;

		private System.Web.UI.CssStyleCollection targetStyle 
		{
			get 
			{
				WebControl targetWebControl = this.targetControl as WebControl;
				if ( targetWebControl != null ) 
				{
					return targetWebControl.Style;
				}
				System.Web.UI.HtmlControls.HtmlControl targetHtmlControl = this.targetControl as System.Web.UI.HtmlControls.HtmlControl;
				if ( targetHtmlControl != null ) 
				{
					return targetHtmlControl.Style;
				}
				return null;
			}
		}
	}
}